import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/common_models/business_attribute_model.dart';

class BusinessAttributeItem extends StatelessWidget {
  final BusinessAttributeModel attributeModel;
  final double fixedWidth;

  const BusinessAttributeItem({Key key, @required this.attributeModel, @required this.fixedWidth}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        buildTitle(context),
        SizedBox(height: 8.0),
        buildAttributeItems(context),
      ],
    );
  }

  Widget buildTitle(BuildContext context) {
    return Text(this.attributeModel.title, style: BaseStyles.bodyMedium(context).copyWith(fontSize: 14, fontWeight: FontWeight.bold));
  }

  Widget buildAttributeItems(BuildContext context) {
    return ListView.separated(
      shrinkWrap: true,
      itemCount: attributeModel.itemList.length,
      physics: NeverScrollableScrollPhysics(),
      itemBuilder: (BuildContext context, int index) => buildAttributeItem(context, attributeModel.itemList[index]),
      separatorBuilder: (BuildContext context, int index) => SizedBox(height: 8.0),
    );
  }

  Widget buildAttributeItem(BuildContext context, AttributeItemModel item) {
    if (!item.showCopied || item.attribute.value == null || item.attribute.value.isEmpty) return buildItem(itemModel: item);
    return InkWell(
      onTap: () => BaseClipboardTool.setDataToast(item.attribute.value ?? ''),
      child: buildItem(itemModel: item),
    );
  }

  Widget buildItem({@required AttributeItemModel itemModel}) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (itemModel.showColon) BaseTextAdaptiveWidthWidget(text: itemModel.attribute.key, fixedWidth: fixedWidth, textStyle: TextStyle(fontSize: 13, color: Color(0xFF868686))),
        if (itemModel.showColon) Text('：', style: TextStyle(fontSize: 13, color: Color(0xFF868686))),
        SizedBox(width: 4),
        if (!itemModel.showColon) Flexible(child: Text("${itemModel.attribute.key} ${itemModel.attribute.value ?? ''}", style: TextStyle(fontSize: 13, color: Color(0xFF868686)))),
        if (itemModel.showColon) Flexible(child: Text("${itemModel.attribute.value ?? ''}", style: TextStyle(fontSize: 13, color: Color(0xFF868686)))),
        if (itemModel.showCopied && (itemModel.attribute.value != null && itemModel.attribute.value.isNotEmpty)) SizedBox(width: 4),
        if (itemModel.showCopied && (itemModel.attribute.value != null && itemModel.attribute.value.isNotEmpty)) Icon(BaseIconData.x3Copy, size: 16),
      ],
    );
  }
}
